# -*- coding: utf-8 -*-
"""
Performance timer.

Created on Wed Apr 29 12:58:31 2020

@author: Farman
"""


import time

class Timer:
    '''
    Usage:
        
        #import MyTimer
        t = MyTimer.Timer()
        t.start()
        # do your job here.
        t.stop()
        t.show()
    '''
    def __init__(self):
        self.time_start = 0
        self.time_stop  = 0
        self.is_running = False
        self.message = 'Timer initialized.'
        return
    
    
    def start(self):
        if not self.is_running:
            self.time_start = time.time()
            self.is_running = True
            self.message = 'Timer is running...'
        else:
            pass
        
        return
    
    
    def stop(self):
        if self.is_running:
            self.time_stop = time.time()
            self.is_running = False
            self.message = 'Timer is stop.'
        else:
            pass
        
        return
    
    
    def get_elapse(self):
        if self.is_running:
            return time.time() - self.time_start
        else:
            return self.time_stop - self.time_start
        
    
    def show(self):
        elapse = self.get_elapse()        
        print('Time used : %s seconds.'%(int(elapse*10.0)/10.0))
        return
    

if __name__ == '__main__':
    print('Usage:')
    print('=' * 30)
    print('''    import MyTimer
    t = MyTimer.Timer()
    t.start()
    
    # do your job here.
    
    t.stop()
    t.show()''')
    print('=' * 30)
    
    '''
    t = Timer()
    t.show()
    print('Timer start ...')
    t.start()

    for n in range(5):
        time.sleep(n)
        t.show()
        
    t.stop()
    print('Timer stop.')
    t.show()
    print(t.get_elapse())
    '''      
